package java8NewFeatures.dateDomo;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

/**
 * @author ldy
 * @data 2022/11/9
 * @description
 */
public class InstantDemo {
    public static void main(String[] args) {
        Instant instant1 = Instant.now();    // 默认获取UTC时区
        System.out.println(instant1);
// 运行结果：2019-10-27T05:59:58.221Z

// 偏移量运算
        OffsetDateTime offsetDateTime = instant1.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
// 运行结果：2019-10-27T13:59:58.221+08:00

// 获取时间戳
        System.out.println(instant1.toEpochMilli());
// 运行结果：1572156145000

// 以Unix元年为起点，进行偏移量运算
        Instant instant2 = Instant.ofEpochSecond(60);
        System.out.println(instant2);
// 运行结果：1970-01-01T00:01:00Z
    }
}
